package com.pompom.workers;

import java.io.File;
import java.io.PrintWriter;

import com.pompom.matchcrawler.MatchFetcher;
import com.pompom.matchcrawler.MatchPersistor;
import com.pompom.matchcrawler.ResponseTypeChecker;

import nl.ansuz.android.steam.dota.response.match.GetMatchDetailsResponse;

public class CrawlerWorker extends AbstractWorker{



	private boolean forward=true; 
	private long currentMatch;
	MatchPersistor mpersistor= new MatchPersistor();

	public CrawlerWorker()
	{

	}


	public void work()
	{




		System.out.println("retrieving match "+currentMatch);
		MatchFetcher fetcher = new MatchFetcher(currentMatch);
		GetMatchDetailsResponse match = fetcher.fetch();

		if (fetcher.getResponseError()==ResponseTypeChecker.NO_ERROR)
		{
			if (MatchPersistor.isMatchAlreadyInDB(currentMatch)!=0)
			{
				System.out.println("Crawler : "+currentMatch+" Already in DB - skip");
			}
			else
			{
				System.out.println("persisting match "+currentMatch);
				mpersistor.persist(match);
			}
		}
		else
			System.out.println("Error "+fetcher.getResponseError());
		System.out.println("done");
		if (forward)
			currentMatch++;
		else
			currentMatch--;






	}


	@Override
	public void setParameters() {
		if (getParameter("forward")!=null && getParameter("forward").equals("false"))
			forward=false;
		if (getParameter("matchid")!=null)
		{
			currentMatch = Long.parseLong(getParameter("matchid"));
		}
	}





}
